package ljye.com.ljye.adapter;

import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import ljye.com.ljye.R;

/**
 * Created by Administrator on 2016/12/29.
 * 热门课程RecyclerView——adapter
 */

 public class Popular_Course_Adapter extends RecyclerView.Adapter<Popular_Course_Adapter.MyViewHolder>{
    private List<String> mlist;
    private FragmentActivity activity;
    public Popular_Course_Adapter(FragmentActivity activity, List<String> list) {
        this.mlist = list;
        this.activity=activity;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.popular_course_item, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.tv.setText(mlist.get(position));
        holder.ivImg.setImageResource(R.drawable.class_item);
//        String url = mlist.get(position).getImg_url();
//        Picasso.with(activity)
//                            .load(url)
//                            .placeholder(R.drawable.default_error)
////                            .fit()//填充全部
//                            .into(holder.ivImg);
        if (mOnItemClickLitener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position1 = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView,position1);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int position1 = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView,position1);
                    return false;
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return mlist.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv;
        ImageView ivImg;
        public MyViewHolder(View view) {
            super(view);
            tv = (TextView) view.findViewById(R.id.tv);

            ivImg = (ImageView) view.findViewById(R.id.iv_img);
        }
    }


    private OnItemClickLitener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public interface OnItemClickLitener
    {
        void onItemClick(View view, int position);
        void onItemLongClick(View view , int position);
    }
}